package com.twt.common.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

public class MD5Util {
    private MD5Util() {
    }
    public static String encode(String text) {
        try {
            // 注册BouncyCastle:
            Security.addProvider(new BouncyCastleProvider());
            // 按名称正常调用:
            MessageDigest md = null;

            md = MessageDigest.getInstance("MD5");

            md.update(text.getBytes("UTF-8"));
            byte[] result = md.digest();
            return new BigInteger(1, result).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
